<?php


    /**
    * @author Ivan Kukic
    * @version 1.0
    * @created 09-Oct-2012 23:29:52
    */
    class colparse
    {
        private $languages;
        private $CI;
        private $strparse;

        public function __construct()
        {
            $this->CI = &get_instance();
            
            // load strparse
            $this->CI->strparse =& $this->CI->factory->create('strparse','foo','bar');

            // Get languages from config  
            foreach(config_item("languages") as $key=>$value):
                $this->languages[] =  $key;
                endforeach;
        }

        /**
        *
        * @param str_to_update
        * @param cur_str
        * @param lang
        */
        function upate_colum($str_to_update, $cur_str, $lang)
        {
            // Create array from language coded string
            $titles = $this->col_to_array($str_to_update);

            // Update array with new string
            if($titles=="No matches") $titles = array();
            $titles[$lang] = $cur_str;

            // Return language coded string
            return $this->array_to_col($titles);
        }

        /**
        * Create array from language coded string
        * @param string
        * @return array
        */
        function col_to_array($string)
        {
            $titles = array();
            foreach($this->languages as $lang):

                // Find string in language coded string for given language
                $str = $this->CI->strparse->find_lang_in_string($string,$lang);
                if($str!=""){
                    $titles[$lang] = $str;
                }
                endforeach;

            // return if empty array
            if(count($titles)==0)return "No matches";

            // return titles as array
            return $titles;
        }

        /**
        * Create language coded string from array('me'=>value,'en'=value...)
        * @param arr
        * @return array
        */
        function asoc_array_to_col($arr)
        {
            $string = "";
            foreach($this->languages as $lang):

                // Create one line of language coded string
                if(isset($arr[$lang])) $string.=$this->CI->strparse->create_lang_string($arr[$lang],$lang);
                endforeach;

            // Return language coded string
            return $string;
        }

        /**
        * Create language coded string from regular array(value,value,value)
        * from post array and langauages are paralel
        * @param arr
        * @return array
        */
        function regular_array_to_col($arr)
        {
            $cnt = count($arr);
            $string = "";

            for($i=0;$i<$cnt;$i++):
                // Replace "" with ''
                $arr[$i] = str_replace("\"", "'", $arr[$i]);

                // Create one line of language coded string
                $string.=$this->CI->strparse->create_lang_string($arr[$i],$this->languages[$i]);
                endfor;

            // Return language coded string
            return $string;
        }
    }
?>